﻿using KinonekoSoftware.CodariaDB.Documents.TemplateContents;
using Verbose = KinonekoSoftware.Studio.Messages.Verbose;

namespace KinonekoSoftware.Studio.Pages.Instances.Templated
{
    public abstract class TemplatedEditorBase : TabViewModelCore, ISaveSupport
    {
        public static readonly TemplatedContentSize[] DefaultContentSize = new[] { TemplatedContentSize.Normal, TemplatedContentSize.Large, TemplatedContentSize.Ultra };

        public const string ID_AdvancedEditor = "AdvancedEditor_{0}";

        private string _name;
        private string _intro;
        private string _avatar;

        protected TemplatedEditorBase()
        {
            ContentSizes = DefaultContentSize;
            Removable    = true;
            SaveCommand  = CreateCommand(DoSaveCommand);
        }

        void ISaveSupport.Save()
        {
            Save();
            SetDirtyState(false);
        }

        protected abstract void Save();
        protected abstract void OnFinish();
        protected abstract void OnLoad();


        private async void DoSaveCommand()
        {
            //
            //
            Xplat.RunOnUIThread(Save);
            
            //
            //
            await Success(TitleSR.SuccessOperation, TextSR.Successful_Save);
        }


        protected override void OnCommandProc(Verbose verbose)
        {
            if (verbose == Verbose.Save)
            {
                Save();
            }

            base.OnCommandProc(verbose);
        }

        protected override void OnReceive(PageContext context)
        {
            if (context.Has(ContextBase.Key_Value))
            {
                InstanceCache = context.GetObject<InstanceCache>(ContextBase.Key_Value);
                NeedCreate    = false;
            }
            else
            {
                NeedCreate = true;
            }
        }


        protected override async void OnStart()
        {
            using (var session = await Ioc.Get<IBusyServiceEX>().BeginSession(""))
            {

                if (NeedCreate)
                {
                    session.Update("正在创建新技能设定...");
                }

                session.Update("正在加载或创建新技能设定...");
                OnLoad();

                //
                //
                session.Update("正在同步技能设定数据...");
                await session.Await();
                OnFinish();

                //
                //
                await session.Await();
            }


            base.OnStart();
        }

        public string Intro
        {
            get => _intro;
            set
            {
                SetValue(ref _intro, value);
                SetDirtyState(true);
            }
        }

        public string Avatar
        {
            get => _avatar;
            set
            {
                SetValue(ref _avatar, value);
                SetDirtyState(true);
            }
        }

        public string Name
        {
            get => _name;
            set
            {
                SetValue(ref _name, value);
                SetDirtyState(true);
            }
        }


        public InstanceCache                     InstanceCache { get; protected set; }
        public bool                              NeedCreate    { get; protected set; }
        public IEnumerable<TemplatedContentSize> ContentSizes  { get; }


        public ICommandEX SaveCommand { get; }
    }

    public abstract class OnlyTemplatedEditor<TTemplated> : TemplatedEditorBase<TTemplated> where TTemplated : TemplatedContent
    {
        
        protected abstract void GetOrCreate(InstanceCache cache, out TTemplated content);

        protected sealed override void OnLoad()
        {
            GetOrCreate(InstanceCache, out _templatedContent);
        }
    }

}